package com.starblink.android.guang.route

import android.content.Context
import android.util.Log
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.starblink.android.basic.user.LoginCheckInterceptor
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.ActivityStackManage

/**
 *  Create by yuanl at 2023/4/26
 **/
@Interceptor(priority = 100, name = "GuangUserIntercept")
class UserIntercept: IInterceptor {

    private var appContext: Context? = null

    private var loginPostCard: Postcard? = null

    override fun init(context: Context?) {
        this.appContext = context
    }

    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
        // Log.d(TAG, "process:  $postcard")
        if (RoutePage.Extra.NEED_LOGIN == postcard?.extra && UserDataCenter.isLogin().not()) {
            loginPostCard = postcard
            LoginCheckInterceptor.check2Login(ActivityStackManage.currentActivity()) {
                if (loginPostCard != null) {
                    callback?.onContinue(loginPostCard)
                }
            }
        } else {
            callback?.onContinue(postcard)
        }
    }

    private companion object {

        const val TAG = "UserIntercept"

    }

}